package com.xy.tianyu.utils

import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.GsonUtils
import com.tencent.mmkv.MMKV
import com.xy.tianyu.constant.Constant
import com.xy.tianyu.module.video.bean.AccessTokenBean
import com.xy.tianyu.module.video.bean.Infovideo
import com.xy.tianyu.module.video.bean.Jrvideo
import java.util.*

/**
 * @time 2020/9/16 14:24
 * @class 描述:
 */
object UniqueUtils {
    private val currentMills = System.currentTimeMillis()
    /**
     * 获取新闻u参数
     */
    fun getNewsU():String{
        var newsU  = MMKV.defaultMMKV().decodeString(Constant.NEWS_U)
        if (newsU.isNullOrEmpty()){
            newsU = UUID.randomUUID().toString()
            MMKV.defaultMMKV().encode(Constant.NEWS_U,newsU)
        }
        return newsU.replace("-","")
    }

    /**
     * 获取新闻uid参数
     */
    fun getNewsUid():String{
        var newsUid  = MMKV.defaultMMKV().decodeString(Constant.NEWS_UID)
        if (newsUid.isNullOrEmpty()){
            newsUid = UUID.randomUUID().toString()
            MMKV.defaultMMKV().encode(Constant.NEWS_UID,newsUid)
        }
        return newsUid.replace("-","")
    }

    /**
     * 获取新闻uid2参数
     */
    fun getNewsUid2():String{
        var newsUid2  = MMKV.defaultMMKV().decodeString(Constant.NEWS_UID2)
        if (newsUid2.isNullOrEmpty()){
            newsUid2 = UUID.randomUUID().toString()
            MMKV.defaultMMKV().encode(Constant.NEWS_UID2,newsUid2)
        }
        return newsUid2.replace("-","")
    }

    /**
     * 获取新闻eid参数
     */
    fun getNewseid():String{
        var newsEid  = MMKV.defaultMMKV().decodeString(Constant.NEWS_EID)
        if (newsEid.isNullOrEmpty()){
            newsEid = UUID.randomUUID().toString()
            MMKV.defaultMMKV().encode(Constant.NEWS_EID,newsEid)
        }
        return newsEid.replace("-","")
    }

    /**
     * 获取新闻csid参数
     */
    fun getNewCsid():String{
        var newsCsid  = MMKV.defaultMMKV().decodeString(Constant.NEWS_CSID)
        if (newsCsid.isNullOrEmpty()){
            newsCsid = UUID.randomUUID().toString()
            MMKV.defaultMMKV().encode(Constant.NEWS_CSID,newsCsid)
        }
        return newsCsid.replace("-","")
    }

    /**
     * 获取视频参数browser_llq_eventmd5
     */
    fun getVideoBrowserLlqEventmd5():String{
        var browserLlqEventmd5First = MMKV.defaultMMKV().decodeString(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_FIRST)
        if (browserLlqEventmd5First.isNullOrEmpty()){
            browserLlqEventmd5First = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_FIRST,browserLlqEventmd5First)
        }

        var browserLlqEventmd5Second = MMKV.defaultMMKV().decodeString(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_SECOND)
        if (browserLlqEventmd5Second.isNullOrEmpty()){
            browserLlqEventmd5Second = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_SECOND,browserLlqEventmd5Second)
        }

        var browserLlqEventmd5Three = MMKV.defaultMMKV().decodeString(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_THREE)
        if (browserLlqEventmd5Three.isNullOrEmpty()){
            browserLlqEventmd5Three = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_THREE,browserLlqEventmd5Three)
        }

        var browserLlqEventmd5Four = MMKV.defaultMMKV().decodeString(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_FOUR)
        if (browserLlqEventmd5Four.isNullOrEmpty()){
            browserLlqEventmd5Four = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.VIDEO_BROWSER_LLQ_EVENTMD5_FOUR,browserLlqEventmd5Four)
        }

        val mutableList = mutableListOf<String>()
        mutableList.add(browserLlqEventmd5First)
        mutableList.add(browserLlqEventmd5Second)
        mutableList.add(browserLlqEventmd5Three)
        mutableList.add(browserLlqEventmd5Four)

        return EncodeUtils.base64Encode2String(GsonUtils.toJson(mutableList).toByteArray())
    }

    /**
     * 获取视频参数access_token
     */
    fun getVideoAccessToken():String{
        var firstUid  = MMKV.defaultMMKV().decodeString(Constant.VIDEO_ACCESS_TOKEN_FIRST_UID)
        if (firstUid.isNullOrEmpty()){
            firstUid = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.VIDEO_ACCESS_TOKEN_FIRST_UID,firstUid)
        }
        var secondUid = MMKV.defaultMMKV().decodeString(Constant.VIDEO_ACCESS_TOKEN_SECOND_UID)
        if (secondUid.isNullOrEmpty()){
            // TODO: 2020/9/27 这个uid很重要，暂时不知道是如何生成的
            secondUid = StringUtils.getRandomNumString(28)+StringUtils.getRandomString(4)
            MMKV.defaultMMKV().encode(Constant.VIDEO_ACCESS_TOKEN_SECOND_UID,secondUid)
        }
        val infovideo = Infovideo(firstUid, currentMills/1000,7776000)
        val  jrvideo = Jrvideo(secondUid,currentMills/1000,7776000)
        val accessTokenBean = AccessTokenBean(infovideo, jrvideo)
        return EncodeUtils.base64Encode2String(GsonUtils.toJson(accessTokenBean).toByteArray())
    }

    fun getNovelIMEI():String{
        var imei  = MMKV.defaultMMKV().decodeString(Constant.NOVEL_IMEI)
        if (imei.isNullOrEmpty()){
            imei = StringUtils.getRandomNumString(15)
            MMKV.defaultMMKV().encode(Constant.NOVEL_IMEI,imei)
        }
        return imei
    }

    fun getNovelUid():String{
        var uid  = MMKV.defaultMMKV().decodeString(Constant.NOVEL_UID)
        if (uid.isNullOrEmpty()){
            uid = StringUtils.getRandomNumString(15)
            MMKV.defaultMMKV().encode(Constant.NOVEL_UID,uid)
        }
        return uid
    }

    fun getNovelMac():String{
        var mac  = MMKV.defaultMMKV().decodeString(Constant.NOVEL_MAC)
        if (mac.isNullOrEmpty()){
            mac = UUID.randomUUID().toString().replace("-","")
            MMKV.defaultMMKV().encode(Constant.NOVEL_MAC,mac)
        }
        return mac
    }
}